from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from import_export import resources
from import_export.admin import ImportExportActionModelAdmin
from simpleui.admin import AjaxAdmin
from import_export.fields import Field
from .models import *


# 药品信息表导入导出文件配置
class ProxyResource(resources.ModelResource):
    # id = Field(attribute='id', column_name='编号')
    # 指定导出时字段显示对应的中文名称
    number = Field(attribute='number', column_name="药品编号")
    name = Field(attribute='name', column_name="药品名")
    nowtotal = Field(attribute='nowtotal',column_name="药品剩余量")
    usedtotal = Field(attribute='usedtotal', column_name="药品用量")
    riskfactor = Field(attribute='riskfactor', column_name="危险性（值越大越危险）")
    detail = Field(attribute='detail', column_name="药品信息")
    class Meta:
        model = Medicine
        # 设置主键字段
        import_id_fields = ('number',)
        # fields是指定哪些字段需要导入
        fields = ('id', 'number', 'name', 'nowtotal', 'usedtotal', 'riskfactor', 'detail')
        # export_order是导出的字段顺序
        export_order = ('id', 'number', 'name', 'nowtotal', 'usedtotal', 'riskfactor', 'detail')


# 药品信息配置
class MedicineAdmin(ImportExportActionModelAdmin, AjaxAdmin):
    resource_class = ProxyResource
    list_display = ['id', 'number', 'name', 'nowtotal', 'usedtotal', 'riskfactor', 'user_risk']
    list_editable = ['nowtotal', 'usedtotal', 'riskfactor', 'user_risk']
    list_per_page = 8
    # 定义生成可下拉查询的字段
    list_filter = ['riskfactor', 'user_risk']
    # list_display_links = ['name', ]点击name字段可以跳转到对应的修改界面
    list_display_links = ['name', ]
    # 定义生成可输入文字查询的搜索框 搜索字段为'number', 'name', 'riskfactor', 'user_risk'
    search_fields = ['number', 'name']


# 借用药品信息表导入导出配置
class ProxyBorrow(resources.ModelResource):
    class Meta:
        model = Borrow
        import_id_fields = ('user', 'boDate')
        fields = ('id', 'user', 'lab', 'medicine', 'medicineUsedNum', 'boDate',)
        export_order = ('id', 'user', 'lab', 'medicine', 'medicineUsedNum', 'boDate')


# 借用药品信息表配置
class BorrowAdmin(ImportExportActionModelAdmin, AjaxAdmin):
    resource_class = ProxyBorrow
    list_display = ['id', 'user', 'medicine', 'medicineUsedNum', 'boDate']
    # 定义了可以点击user字段跳转到对应的修改界面
    list_display_links = ['user', ]
    # 定义生成可输入文字查询的搜索框 搜索字段为user用户名
    search_fields = ['user']
    # 定义生成了可下拉查询的搜索框
    list_filter = ['medicine', 'boDate']
    list_per_page = 10


#  TabularInline，绑定父子模型之间的关系
# User对象的同时也能编辑相关的CommonUser信息
# TabularInline：允许在父模型（这里是User）的编辑页面中以内联表格的形式编辑子模型
#  Django Admin 界面的内联模型管理类
class CommonUserInline(admin.TabularInline):
    # 指定了这个内联类所关联的模型是CommonUser。在父模型的编辑页面上，会显示与该用户相关联的所有 CommonUser 记录。
    model = CommonUser
    # 不允许直接从内联表单中删除CommonUser记录。
    can_delete = False
    # 定义了在 Django Admin 界面中显示的名字
    verbose_name = '用户信息表'
    verbose_name_plural = verbose_name


#  继承BaseUserAdmin并指定inlines属性，将CommonUserInline添加到User模型的Admin界面中
class UserAdmin(BaseUserAdmin):
    inlines = (CommonUserInline,)


# 实验室信息表导入导出配置
class ProxyLabAdmin(resources.ModelResource):
    class Meta:
        model = Lab
        import_id_fields = ('id', 'name')
        fields = ('id', 'name', 'description')
        export_order = ('id', 'name', 'description')


# 实验室信息表配置
class LabAdmin(ImportExportActionModelAdmin, AjaxAdmin):
    resource_class = ProxyLabAdmin
    list_display = ['id', 'name', 'description']
    # list_editable = ['name']
    list_display_links = ['name', ]
    # 定义生成可文字搜索的输入框 查询的字段为name实验室名称
    search_fields = ['name']

    list_per_page = 5
    ordering = ['id']


# # 实验室信息表配置
# class LabAdmin(admin.ModelAdmin):
#     list_display = ['id', 'name', 'description']
#     list_editable = ['name']
#     list_per_page = 10
#     ordering = ['id']


# 仪器信息表导入导出配置
class ProxyInstrumentAdmin(resources.ModelResource):
    class Meta:
        model = Instrument
        import_id_fields = ('id',)
        fields = ('id', 'ins_no', 'ins_name', 'ins_nowtotal', 'ins_bototal', 'ins_detail')
        export_order = ('id', 'ins_no', 'ins_name', 'ins_nowtotal', 'ins_bototal', 'ins_detail')


# 仪器信息表配置
# class InstrumentAdmin(admin.ModelAdmin):
class InstrumentAdmin(ImportExportActionModelAdmin, AjaxAdmin):
    resource_class = ProxyInstrumentAdmin
    list_display = ['id', 'ins_no', 'ins_name', 'ins_nowtotal', 'ins_bototal', 'ins_detail']
    list_per_page = 10
    list_display_links = ['ins_name', ]
    # 定义生成可输入文字查询的搜索框 搜索字段为ins_name仪器名称
    search_fields = ['ins_name']
    list_editable = ['ins_nowtotal', 'ins_bototal']
    ordering = ['ins_no']


# 仪器借用信息表导入导出配置
class ProxyInsBorrowAdmin(resources.ModelResource):
    class Meta:
        model = InsBorrow
        import_id_fields = ('id',)
        fields = ('id', 'ins_user', 'instrument', 'ins_boNum', 'ins_boDate', 'ins_tuData')
        export_order = ('id', 'ins_user', 'instrument', 'ins_boNum', 'ins_boDate', 'ins_tuData')


# 仪器借用信息表配置
class InsBorrowAdmin(ImportExportActionModelAdmin, AjaxAdmin):
    resource_class = ProxyInsBorrowAdmin
    list_display = ['id', 'ins_user', 'instrument', 'ins_boNum', 'ins_boDate', 'ins_tuData']
    list_per_page = 10
    # 定义生成可文字搜索的输入框 查询的字段为ins_user用户名
    search_fields = ['ins_user']
    # 定义生成可下拉搜索的输入框 查询的字段为instrument借用仪器名称
    list_filter = ['instrument']
    list_display_links = ['ins_user', ]
    ordering = ['-ins_boDate']


# 文件信息表配置
class DocAdmin(admin.ModelAdmin):
    list_display = ['id', 'file_name', 'file_obj', 'upload_time']
    list_display_links = ['file_name', ]
    # 定义生成可查询的字段
    search_fields = ['file_name']

    list_per_page = 10
    ordering = ['-upload_time']


# 注册模型到后台管理系统
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
admin.site.register(Medicine, MedicineAdmin)
admin.site.register(Borrow, BorrowAdmin)
admin.site.register(Lab, LabAdmin)
admin.site.register(Instrument, InstrumentAdmin)
admin.site.register(InsBorrow, InsBorrowAdmin)
admin.site.register(Doc, DocAdmin)

admin.site.site_header = '实验药品管理'
admin.site.site_title = '实验药品管理'
